﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using TTTTBHYT.VO;

namespace TTTTBHYT.DAO
{
    class Mau29DetailDAO
    {
        private dbConnection conn;
        Random rnd = new Random();
        public Mau29DetailDAO()
        {
            conn = new dbConnection();
        }
        public DataTable SearchBySoKCB(string KeyGH)
        {
            string query = string.Format("select * from MAU29_DTL "
            + " where KeyGH = @keygh");
            SqlParameter[] sqlParameters = new SqlParameter[1];
            sqlParameters[0] = new SqlParameter("@keygh", SqlDbType.NVarChar);
            sqlParameters[0].Value = Convert.ToString(KeyGH);
            return conn.executeSelectQuery(query, sqlParameters);
        }
        public MAU29DETAILVO InsertRecord(MAU29DETAILVO Mau29DetailVO)
        {
            string query = string.Format("INSERT INTO [TTTTBHYT_NT].[dbo].[MAU29_DTL]([KEYSL], [KEYGH], [NGAY], [TIENCHIPHI], [TIENKCB], [LYDO],[KETLUAN]) " 
            + " VALUES(@keysl,@keygh, @ngay, @tienchiphi,@tienkcb,@lydo,@ketluan)");
            SqlParameter[] sqlParameters = new SqlParameter[7];

            sqlParameters[0] = new SqlParameter("@keysl", SqlDbType.NVarChar);
            sqlParameters[0].Value = Convert.ToString(Module.CreateRunninNo(rnd));

            sqlParameters[1] = new SqlParameter("@keygh", SqlDbType.NVarChar);
            sqlParameters[1].Value = Convert.ToString(Mau29DetailVO.KeyGH);

            sqlParameters[2] = new SqlParameter("@ngay", SqlDbType.NVarChar);
            sqlParameters[2].Value = Convert.ToString(Mau29DetailVO.Ngay);

            sqlParameters[3] = new SqlParameter("@tienchiphi", SqlDbType.Int);
            sqlParameters[3].Value = Convert.ToString(Mau29DetailVO.TienChiPhi);

            sqlParameters[4] = new SqlParameter("@tienkcb", SqlDbType.Int);
            sqlParameters[4].Value = Convert.ToString(Mau29DetailVO.TienKCB);

            sqlParameters[5] = new SqlParameter("@lydo", SqlDbType.NVarChar);
            sqlParameters[5].Value = Convert.ToString(Mau29DetailVO.LyDo);

            sqlParameters[6] = new SqlParameter("@ketluan", SqlDbType.NVarChar);
            sqlParameters[6].Value = Convert.ToString(Mau29DetailVO.KetLuan);
            
            if (conn.executeInsertQuery(query, sqlParameters))
            {
                return Mau29DetailVO;
            }
            else
            {
                return null;
            }

        }
        public bool UpdateRecord(MAU29DETAILVO Mau29DetailVO)
        {
            string query = string.Format("UPDATE [TTTTBHYT_NT].[dbo].[MAU29_DTL] "
            + "SET [KEYSL]=@keysl, [KEYGH]=@keygh, [NGAY]=@ngay, "
            + "[TIENCHIPHI]=@tienchiphi, [TIENKCB]=@tienkcb, [LYDO]=@lydo,[KETLUAN]=@ketluan "
            + "WHERE keysl = @keysl)");
            SqlParameter[] sqlParameters = new SqlParameter[7];

            sqlParameters[0] = new SqlParameter("@keysl", SqlDbType.NVarChar);
            sqlParameters[0].Value = Convert.ToString(Module.CreateRunninNo(rnd));

            sqlParameters[1] = new SqlParameter("@keygh", SqlDbType.NVarChar);
            sqlParameters[1].Value = Convert.ToString(Mau29DetailVO.KeyGH);

            sqlParameters[2] = new SqlParameter("@ngay", SqlDbType.NVarChar);
            sqlParameters[2].Value = Convert.ToString(Mau29DetailVO.Ngay);

            sqlParameters[3] = new SqlParameter("@tienchiphi", SqlDbType.Int);
            sqlParameters[3].Value = Convert.ToString(Mau29DetailVO.TienChiPhi);

            sqlParameters[4] = new SqlParameter("@tienkcb", SqlDbType.Int);
            sqlParameters[4].Value = Convert.ToString(Mau29DetailVO.TienChiPhi);

            sqlParameters[5] = new SqlParameter("@lydo", SqlDbType.NVarChar);
            sqlParameters[5].Value = Convert.ToString(Mau29DetailVO.LyDo);

            sqlParameters[6] = new SqlParameter("@ketluan", SqlDbType.NVarChar);
            sqlParameters[6].Value = Convert.ToString(Mau29DetailVO.KetLuan);

            if (conn.executeInsertQuery(query, sqlParameters))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public bool DeleteRecord(string KeyGH)
        {
            string query = string.Format("Delete MAU29_DTL Where keygh = @keygh");
            SqlParameter[] sqlParameters = new SqlParameter[1];
            sqlParameters[0] = new SqlParameter("@keygh", SqlDbType.NVarChar);
            sqlParameters[0].Value = Convert.ToString(KeyGH);
            return conn.executeInsertQuery(query, sqlParameters);

        }
        
    }
}
